//! @Author: DengLibin
//! @Date: Create in 2024-07-18 10:42:44
//! @Description: 测试用
//!

use std::{sync::Arc, thread, time::Duration};

use axum::{routing::get, Extension, Json, Router};
use serde_json::{json, Value};

use crate::{
    app::{self, AppState},
    web::Resdata,
};

pub async fn route(router: Router) -> Router {
    //路由
    let mut router = router.route("/task/blocking_task", get(blocking_task));
    router = router.route("/task/task", get(task));

    router
}
/// @Author: DengLibin
/// @Date: Create in 2024-07-18 11:04:39
/// @Description: 测试后台执行阻塞任务
pub async fn blocking_task(Extension(state): Extension<Arc<AppState>>) -> Json<Value> {
    for i in 0..20 {
        app::execute_blocking_task(
            move || {
                //模拟阻塞
                thread::sleep(Duration::from_secs(1));
            
            },
            format!("任务:{}", i),
            state.clone(),
        )
        .await;
    }
    demo_task().await;

    Json(json!(Resdata::<String>::ok("成功".into())))
}

/// @Author: DengLibin
/// @Date: Create in 2024-07-18 11:04:50
/// @Description: 测试后台执行非阻塞任务
pub async fn task(Extension(state): Extension<Arc<AppState>>) -> Json<Value> {
    for i in 0..100 {
        app::execute_task(
            move || {
                //模拟阻塞
                thread::sleep(Duration::from_secs(3));
                
            },
            format!("任务:{}", i),
            state.clone(),
        )
        .await;
    }
    demo_task().await;
    Json(json!(Resdata::<String>::ok("成功".into())))
}


async fn demo_task(){
    println!("执行演示任务");
}